DECLARE @LASTDATE NVARCHAR(8) = CONVERT(NVARCHAR(6), (DATEADD(YY, -1, (DATEADD(YY, DATEDIFF(YY,0,GETDATE()) + 1, -1)))),112)
DECLARE @DATE_UPDATE NVARCHAR(100)= (SELECT [DATE_UPDATE] FROM [DATE_EM_REPORT])
--PRINT(@LASTDATE)

	delete from  DOANH_SO_LC_THEO_THANG_2016

	IF OBJECT_ID('TEMPDB.DBO.#lc_tam') IS NOT NULL
	  DROP TABLE #lc_tam 

	select ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Debit_value_date) AS RN,* 
	into #lc_tam 
	from SERVER12.ANHCP.[DBO].[DOANH_SO_TTR_LC_DP] where year_month > @LASTDATE and loai_giao_dich = 'LC' and cif in(select cif from TBL_ACCOUNT_PLANNING)


	insert DOANH_SO_LC_THEO_THANG_2016
	select (amt_bill*1e9) as AMOUNT_QD, cif, year_month  from #lc_tam where rn=1